﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using MicroConfig.NetCore.Server.Models;
using MicroConfig.NetCore.Server.Services;
using Microsoft.AspNetCore.SignalR;

namespace MicroConfig.NetCore.Server.Controllers
{
    [AuthFilter]
    public class HomeController : Controller
    {
        private readonly UserService userService;
        private readonly ProjectService projectService;
        private readonly ConfigItemService configItemService;

        private readonly IHubContext<MicroConfigHub> hubContext;

        public HomeController(UserService userService,
            ProjectService projectService,
            ConfigItemService configItemService,
            IHubContext<MicroConfigHub> hubContext)
        {
            this.userService = userService;
            this.projectService = projectService;
            this.configItemService = configItemService;
            this.hubContext = hubContext;
        }

        [AuthFilter(Anonymous = true)]
        public IActionResult Login()
        {
            return View();
        }

        [HttpPost]
        [AuthFilter(Anonymous = true)]
        public IActionResult doLogin(string account, string password)
        {
            var user = userService.Single(account, password);
            if (user == null)
            {
                return Content("0");
            }
            else
            {
                var token = EncryptHelper.EncryptString(user.Account + "___" + user.Password);
                Response.Cookies.Append("_auth_", token, new Microsoft.AspNetCore.Http.CookieOptions
                {
                    Path = "/",
                    IsEssential = true,
                });
                return Content("1");
            }
        }

        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Project()
        {
            return View();
        }
        public IActionResult ApiAllProject(string kw)
        {
            var list = projectService.SelectAll(kw);
            return Json(list);
        }
        [HttpPost]
        public IActionResult ApiEditProject(Project m)
        {
            var ro = projectService.Edit(m);
            return Json(ro);
        }
        [HttpPost]
        public IActionResult ApiDeleteProject(long id)
        {
            var ro = projectService.Delete(id);
            return Json(ro);
        }


        public IActionResult UpdateAccount()
        {
            return View();
        }
        [HttpPost]
        public IActionResult DoUpdateAccount(User user)
        {
            var flag = userService.Update(user);

            return Content(flag.ToString());
        }

        public IActionResult ItemPage(long? projectId, string kw, int page = 1)
        {
            var plist = configItemService.SelectPage(projectId, kw, page, 10);
            return Json(plist);
        }
        [HttpPost]
        public IActionResult DeleteItem(long id)
        {
            var ro = configItemService.Delete(id);
            return Json(ro);
        }
        [HttpPost]
        public IActionResult AddItem(ConfigItem m)
        {
            m.Value = m.Value ?? string.Empty;
            m.Environment = m.Environment ?? string.Empty;
            m.Key = m.Key ?? string.Empty;
            var ro = configItemService.Add(m);
            //sync
            if (ro.Success)
            {
                hubContext.Clients.Group(m.ProjectId.ToString()).SendAsync("UpdateItem", new UpdateItemParams
                {
                    Env = m.Environment ?? string.Empty,
                    Key = m.Key,
                    Value = m.Value
                });
            }

            return Json(ro);
        }

        [HttpPost]
        public IActionResult UpdateItem(ConfigItem m)
        {
            m.Value = m.Value ?? string.Empty;
            m.Environment = m.Environment ?? string.Empty;
            m.Key = m.Key ?? string.Empty;
            var ro = configItemService.Update(m.Id, m.Value);
            //sync
            if (ro.Success)
            {
                hubContext.Clients.Group(m.ProjectId.ToString()).SendAsync("UpdateItem", new UpdateItemParams
                {
                    Env = m.Environment,
                    Key = m.Key,
                    Value = m.Value
                });
            }
            return Json(ro);
        }

    }
}
